package com.hera.base.web.vaadin.mantenimiento;

import static com.hera.base.util.InputData.getStringData;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.form.domain.FieldOption;
import com.hera.process.form.domain.FormField;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class FieldOptionUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_5;
	@AutoGenerated
	private Button btnEliminar;
	@AutoGenerated
	private Button btnNuevo;
	@AutoGenerated
	private TextField txtLabel;
	@AutoGenerated
	private TextField txtValue;
	@AutoGenerated
	private Label lbltitulo;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	//	private FormField formField;
	private Long idField;
	
     FormField formField= new FormField();
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
     public FieldOptionUI() {
		this(null);
	}
	public FieldOptionUI(Long idField) {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		this.idField = idField;
	}
	
	private static final Logger log = Logger.getLogger(StageUserUI.class);
	
	@Autowired
	FormDelegate formDelegate;
	boolean esNuevo;
	boolean esNuevoDetalle;
	FieldOption fieldoption;
	
	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getButton().equals(btnEliminar)) {
			eliminarMaestro();
		} else if(event.getButton().equals(btnNuevo)){
            guardarMaestro();
		}
		
	}

	@Override
	public void cargarFormulario() {
		
//		textIdField.setNullRepresentation("");
		txtValue.setNullRepresentation("");
		txtLabel.setNullRepresentation("");
		 
		log.info("+++++++");
		esNuevo=true;
		fieldoption=null;

		btnEliminar.setVisible(false);
		btnEliminar.addClickListener(this);
		
		btnNuevo.setVisible(false);
		btnNuevo.addClickListener(this);
		btnNuevo.setCaption("Nuevo");
		
		tblfMaestro.setTitulo("Lista de Opciones");
		tblfMaestro.setHeight("200px");
		
//		tblfMaestro.addColumn("formField.idField", "idField", 30, true, String.class);
		tblfMaestro.addColumn("value", "value", 50, true, String.class);
		tblfMaestro.addColumn("label", "label", 70, true, String.class);
		
		tblfMaestro.construir();
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 5537095084983335605L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);
				
			}
		});
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				fieldoption=(FieldOption)tblfMaestro.getSelectedItem(event);
				esNuevo=(fieldoption==null);
				tblfMaestro.limpiarFiltro();
				if (esNuevo) {
					limpiarMaestro();
					btnEliminar.setVisible(false);
					
					
				} else {
                    btnEliminar.setVisible(true);
                    btnNuevo.setVisible(true);
                    btnNuevo.setCaption("Modificar");
//                    textIdField.setValue(""+fieldoption.getFormField().getIdField());
                    txtValue.setValue(fieldoption.getValue());
                    txtLabel.setValue(fieldoption.getLabel());
				}
				log.info("Fin Cargar Formulario FIELDOPTION ");
				
			}
		});
		
	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
		
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio Buscar FieldOption");
		FiltroDTO filtroDTO=tblfMaestro.getFiltroDTO(event);
		filtroDTO.addParametro("formField.idField", idField);
		List<FieldOption> lstFieldOptions=formDelegate.findAllFieldOptions(filtroDTO);
		tblfMaestro.setLstData(lstFieldOptions);
		limpiarMaestro();
		log.info("fin FindFieldOption");
	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarMaestro");
		btnNuevo.setVisible(true);
		btnNuevo.setCaption("Nuevo");
//		textIdField.setValue("");
		txtValue.setValue("");
		txtLabel.setValue("");
		log.info("fin limpiarMaestro");
		
	}

	@Override
	public void eliminarMaestro() {
		log.info("inicio eliminarMaestro");
		formDelegate.deleteFieldOption(fieldoption);
		buscarMaestro();
		limpiarMaestro();
		log.info("fin eliminarMaestro");
		
	}

	@Override
	public void guardarMaestro() {
		log.info("inicio guardarMaestro");
		if (esNuevo) {
			fieldoption=new FieldOption();
			obtenerDatosFormulario(fieldoption);
			formDelegate.createFieldOption(fieldoption);
		}else{
			obtenerDatosFormulario(fieldoption);
			formDelegate.updateFieldOption(fieldoption);
		}
		limpiarMaestro();
		buscarMaestro();
		log.info("fin guardarMaestro");
	}

	@Override
	public void buscarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void limpiarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eliminarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void guardarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		
	}
	
	private void obtenerDatosFormulario(FieldOption fieldoption){
        
		fieldoption.setFormField(new FormField());
		fieldoption.getFormField().setIdField(idField);
		fieldoption.setValue(getStringData(txtValue,"VALUE"));
		fieldoption.setLabel(getStringData(txtLabel,"LABEL"));
	}
	

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("-1px");
		setHeight("-1px");
		
		// verticalLayout_1
		verticalLayout_1 = buildVerticalLayout_1();
		mainLayout.addComponent(verticalLayout_1);
		
		return mainLayout;
	}
	@AutoGenerated
	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("-1px");
		verticalLayout_1.setHeight("-1px");
		verticalLayout_1.setMargin(false);
		verticalLayout_1.setSpacing(true);
		
		// lytMaestro
		lytMaestro = buildLytMaestro();
		verticalLayout_1.addComponent(lytMaestro);
		
		return verticalLayout_1;
	}
	@AutoGenerated
	private HorizontalLayout buildLytMaestro() {
		// common part: create layout
		lytMaestro = new HorizontalLayout();
		lytMaestro.setImmediate(false);
		lytMaestro.setWidth("-1px");
		lytMaestro.setHeight("-1px");
		lytMaestro.setMargin(false);
		lytMaestro.setSpacing(true);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lytMaestro.addComponent(lytTablaMaestra);
		
		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		lytMaestro.addComponent(pnlFormulario);
		
		return lytMaestro;
	}
	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("-1px");
		lytTablaMaestra.setHeight("-1px");
		lytTablaMaestra.setMargin(false);
		
		// tblfMaestro
		tblfMaestro = new TableCustom();
		tblfMaestro.setImmediate(false);
		tblfMaestro.setWidth("-1px");
		tblfMaestro.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfMaestro);
		
		return lytTablaMaestra;
	}
	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("-1px");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);
		pnlFormulario.setSpacing(true);
		
		// lbltitulo
		lbltitulo = new Label();
		lbltitulo.setImmediate(false);
		lbltitulo.setWidth("-1px");
		lbltitulo.setHeight("-1px");
		lbltitulo.setValue("Opciones");
		pnlFormulario.addComponent(lbltitulo);
		
		// txtValue
		txtValue = new TextField();
		txtValue.setImmediate(false);
		txtValue.setDescription("value");
		txtValue.setWidth("-1px");
		txtValue.setHeight("-1px");
		txtValue.setInputPrompt("value");
		pnlFormulario.addComponent(txtValue);
		
		// txtLabel
		txtLabel = new TextField();
		txtLabel.setImmediate(false);
		txtLabel.setDescription("Label");
		txtLabel.setWidth("-1px");
		txtLabel.setHeight("-1px");
		txtLabel.setInputPrompt("Label");
		pnlFormulario.addComponent(txtLabel);
		
		// horizontalLayout_5
		horizontalLayout_5 = buildHorizontalLayout_5();
		pnlFormulario.addComponent(horizontalLayout_5);
		
		return pnlFormulario;
	}
	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_5() {
		// common part: create layout
		horizontalLayout_5 = new HorizontalLayout();
		horizontalLayout_5.setImmediate(false);
		horizontalLayout_5.setWidth("-1px");
		horizontalLayout_5.setHeight("-1px");
		horizontalLayout_5.setMargin(false);
		horizontalLayout_5.setSpacing(true);
		
		// btnNuevo
		btnNuevo = new Button();
		btnNuevo.setCaption("Nuevo");
		btnNuevo.setImmediate(true);
		btnNuevo.setWidth("-1px");
		btnNuevo.setHeight("-1px");
		horizontalLayout_5.addComponent(btnNuevo);
		
		// btnEliminar
		btnEliminar = new Button();
		btnEliminar.setCaption("Eliminar");
		btnEliminar.setImmediate(true);
		btnEliminar.setWidth("-1px");
		btnEliminar.setHeight("-1px");
		horizontalLayout_5.addComponent(btnEliminar);
		
		return horizontalLayout_5;
	}


}
